<?php
/**
 * Created by PhpStorm.
 * User: 86151
 * Date: 2024/3/5
 * Time: 14:38
 */

namespace app\admin\model;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Config;
use think\Db;
use think\Log;
use think\Model;
class Index extends Model
{
    //只支持117位加密
    public static function setEncrypt($public_key,$data){
        $encrypted = "";
        $pu_key = openssl_pkey_get_public($public_key);
        openssl_public_encrypt($data,$encrypted,$pu_key);
        $encrypted = base64_encode($encrypted);
        return $encrypted;

    }

//超长字符加密 返回数值
    public static function setLongEncrypt($public_key,$data){
        $listdata=Array();
        $subject=$data;
        $spr=str_split($subject,116);
        for($i=0;$i<count($spr);$i++){
            $srt=self::setEncrypt($public_key,$spr[$i]);
            array_push($listdata,$srt);
        }
        return $listdata;
    }

//解密
    public static function setDecrypt($private_key,$data){
        $decrypted = "";
        $pi_key =  openssl_pkey_get_private($private_key);
        //这个函数可用来判断私钥是否是可用的，可用返回资源id Resource id
        openssl_private_decrypt(base64_decode($data),$decrypted,$pi_key);

        return $decrypted;
    }
//数组解密
    public static function setDecryptArray($private_key,$arraydata){

        $DecryptStr="";
        for($i=0;$i<count($arraydata);$i++){
            $DecryptStr=$DecryptStr.self::setDecrypt($private_key,$arraydata[$i]);
        }
        return $DecryptStr;
    }

    /**
     * 每日精选阿里推荐,按城市获取,销量排序
     * @param $city
     */
    static public function aliList($city='',$cat_code=0,$page=1,$page_size=20,$erji_id,$content=''){
        $city_id = Db::name("ali_city")->where(['city_name'=>$city])->value('city_code');
        $map['switch'] = 1;
        if($city_id){
            //能定位到城市的话,就推荐相关城市的
            $map['apply_city_ids'] = ['like',"%$city_id%"];
        }
        $rdd=[];
        if($content){
            $map['title'] = ['like',"%$content%"];
        }
        if($erji_id){
            $rdd['erji'] = $erji_id;
        }
        if($cat_code){
            $cat_codes = Db::name("ali_category")->where('yiji',$cat_code)->where($rdd)->where('switch',1)->column('cat_code');
            if(!$cat_codes){
                $map['switch'] = 5;
            }
            $map['tb_category_3_id'] = ['in',$cat_codes];//
        }
        $data = Db::name("goods")
            ->field(['item_id','main_picture','title','original_price_cent','tb_category_3_name as category','activity_price_cent','total_sales','type'])
            ->where($map)
            ->page($page,$page_size)
            ->where('type','<>',2)
            ->order('total_sales DESC')
            ->select();
        $count = Db::name("goods")->where($map)->count();
        if(!empty($data)){
            foreach($data as $k=>$v){
                $data[$k]['original_price_cent'] = round($v['original_price_cent']/100,2);
                $data[$k]['activity_price_cent'] = round($v['activity_price_cent']/100,2);
            }
        }
        return [
            'list'=>$data,
            'count'=>$count
        ];
    }

    /**
     * 权益列表的数据渲染
     * @param $data
     * @return array
     */
    static public function getAnyTypeData($data,$user_id=0){
        if(!empty($data)){
            foreach($data as $k=>$v){
                switch($v['type']){
                    case 1:
                        //阿里物品
                        $result = Db::name("goods")->where(['item_id'=>$v['resource_id']])->find();
                        $data[$k]['title'] = $v['resource_names'];
                        $data[$k]['brand'] = json_decode($result['brand'],true)['brand_name'];
                        $data[$k]['goods_type'] = $result['type'];
                        $data[$k]['main_picture'] = !empty($v['image'])?$v['image']:$result['main_picture'];//有自定义封面优先
                        break;
                    case 2:
                        if($user_id) {
                            $tomorrow = date('Y-m-d', strtotime(date('Y-m-d') . ' +1 day'));
                            $CtripPrice = self::orderrxcj($v['resource_id'],$tomorrow);
                            if ($CtripPrice>0) {
                                $data[$k]['fsj'] = number_format($CtripPrice,2);
                                $data[$k]['vip_money'] =number_format($CtripPrice - $v['xc_price'],2);
                                if($data[$k]['vip_money']<=0){
                                    $data[$k]['vip_money'] = '0.00';
                                }
                            }
                        }
                        //携程物品
//                        $result = Db::name("ctrip_resources")->where(['resouceid'=>$v['resource_id']])->find();
                        $scenicspotid = Db::name('resources_ctrip')->where('resouceid',$v['resource_id'])->find();
                        $data[$k]['scenicspotid'] =$scenicspotid['scenicspotid'];
                        $data[$k]['title'] = $v['resource_names'];
                        $data[$k]['goods_type'] = 3;
                        $data[$k]['main_picture'] = $v['image'];
                        break;
                    case 3:
                        //代金券
//                        $result = Db::name("coupon")->where(['id'=>$v['resource_id']])->find();
                        $data[$k]['title'] = $v['resource_names'];
                        $data[$k]['main_picture'] = $v['image'];
                        $data[$k]['goods_type'] = 4;//
                        break;
                }
            }
            return $data;
        }else{
            return [];
        }

    }


    /**
     *  验证码接口
     */
    public static function code($mobile,$message){
        $userid =Config::get("operators.userid");
        $passwordMd5 =Config::get("operators.passwordMd5");
        $msg_fmt = 8;
        $url ='http://139.129.107.160:8085/sendsms.php?';
        $curpost = "userid=".$userid."&passwordMd5=".$passwordMd5."&msg_fmt=".$msg_fmt."&message=".$message."&mobile=".$mobile;
        $result = file_get_contents($url.$curpost);
        return $result;
    }
    /**
     * 订单限制
     */
    static public function cbxzorder($item_id,$user_id){
        $firstday = date('Y-m-d');
        $mapdds['from'] =1;
        $mapdds['resource_id'] =$item_id;
        $mapdds['createtime'] = ['between',"{$firstday} 00:00:00,{$firstday} 23:59:59"];
        $mapdds['user_id'] = $user_id;
        $order = Db::name('order')->where($mapdds)->where('status','<>',3)->find();
        if($order){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 订单限制
     */
    static public function ddxzorder($item_id,$mobile){
        $firstday = date('Y-m-d');
        $mapdds['from'] =1;
        $mapdds['resource_id'] =$item_id;
        $mapdds['createtime'] = ['between',"{$firstday} 00:00:00,{$firstday} 23:59:59"];
        $order = Db::name('order')->where($mapdds)->where('status','<>',3)->count();
        if($order >= 5){
            $mssg = '【和悦乐惠】用户'.$mobile.'，已购买商品超过'.$order.'次，请尽快查看是否异常';
            self::code(15182050531,$mssg);//张
            self::code(17343147813,$mssg);//廖
            self::code(15928841814,$mssg);//杨
            self::code(15928773295,$mssg);//王
            return 1;
        }else{
            return 0;
        }
    }
    static public function orderrxcj($resouceid,$add_time){
        $ctripprice = 0;
        $res = Ctrip::send('menpiaojiagerili',[
            'ResourceIDList'=>[$resouceid],
            'StartDate'=>$add_time,
            'EndDate'=>$add_time,
            'SID'=>Config::get("ctrip.sid"),
            'AllianceID'=>Config::get("ctrip.aid")
        ]);
        if(isset($res['ResourceList'][0]['DateList'])){
            $ctripprice = $res['ResourceList'][0]['DateList'][0]['CtripPrice'];
        }
        return $ctripprice;
    }

}